//
//  HWSwitch.m
//  OCDome
//
//  Created by 李含文 on 2020/10/12.
//  Copyright © 2020 李含文. All rights reserved.
//

#import "HWSwitch.h"


@interface HWSwitch()

/* 记录开关状态 */
@property (nonatomic, assign) BOOL isOn;
/* <#name#> */
@property (nonatomic, strong) UIControl *contentView;
/* 滑块view */
@property (nonatomic, strong) UIView *roundView;
@end

@implementation HWSwitch

- (instancetype)initWithCoder:(NSCoder *)aDecoder {
    if (self = [super initWithCoder:aDecoder]) {
        [self initUI];
        [self creatView];
        [self initData];
        [self addObserver];
    }
    return self;
}
- (instancetype)initWithFrame:(CGRect)frame {
    if (self = [super initWithFrame:frame]) {
        [self initUI];
        [self creatView];
        [self initData];
        [self addObserver];
    }
    return self;
}

- (void)initUI {
    self.contentView = [[UIControl alloc] initWithFrame:self.bounds];
    self.contentView.backgroundColor = UIColor.whiteColor;
    self.contentView.clipsToBounds = YES;
    self.roundView = [[UIView alloc]initWithFrame:CGRectMake(0, self.spacing, self.bounds.size.height-self.spacing*2, self.bounds.size.height-self.spacing*2)];
    self.roundView.userInteractionEnabled = NO;
    [self addSubview:self.contentView];
    [self addSubview:self.roundView];
}
- (void)initData {
    self.spacing = 2.0;
    self.backgroundColor = [UIColor whiteColor];
    self.sliderColor = UIColor.whiteColor;
    self.defaultColor = [UIColor colorWithRed:229/255.0 green:229/255.0 blue:229/255.0 alpha:1];
    self.selectedColor = [UIColor colorWithRed:231/255.0 green:39/255.0 blue:34/255.0 alpha:1];
    self.contentborderColor = [UIColor colorWithRed:229/255.0 green:229/255.0 blue:229/255.0 alpha:1];
    self.contentborderWidth = .5;
    self.on = YES;
}
- (void)addObserver {
    [self.contentView addObserver:self forKeyPath:@"isOn" options:NSKeyValueObservingOptionOld | NSKeyValueObservingOptionNew context:nil];
    [self.contentView addTarget:self action:@selector(translateAni) forControlEvents:UIControlEventTouchUpInside];
}
- (void)creatView {
//    self.roundView.layer.shadowColor = [UIColor blackColor].CGColor;//shadowColor阴影颜色
//    self.roundView.layer.shadowOffset = CGSizeMake(0,0);//shadowOffset阴影偏移,x向右偏移4，y向下偏移4，默认(0, -3),这个跟shadowRadius配合使用
//    self.roundView.layer.shadowOpacity = 0.1;//阴影透明度，默认0
//    self.roundView.layer.shadowRadius = 2;//阴影半径，默认3
}

- (void)setContentborderColor:(UIColor *)contentborderColor {
    _contentborderColor = contentborderColor;
    self.contentView.layer.borderColor = contentborderColor.CGColor;
}

- (void)setContentborderWidth:(CGFloat)contentborderWidth {
    _contentborderWidth = contentborderWidth;
    self.contentView.layer.borderWidth = contentborderWidth;
}
- (void)setSpacing:(CGFloat)spacing {
    if (spacing > self.bounds.size.height/4) {
        spacing = self.bounds.size.height/4;
    }
    _spacing = spacing;
    self.on = _isOn;
}
- (void)setSliderColor:(UIColor *)sliderColor {
    _sliderColor = sliderColor;
    self.roundView.backgroundColor = sliderColor;
}

- (void)setDefaultColor:(UIColor *)defaultColor {
    _defaultColor = defaultColor;
    [self setColor];
}
- (void)setSelectedColor:(UIColor *)selectedColor {
    _selectedColor = selectedColor;
    [self setColor];
}
-(void)setOn:(BOOL)on {
    CGFloat x = on ? self.bounds.size.width-(self.bounds.size.height-self.spacing*2)-self.spacing : self.spacing;
    self.roundView.frame = CGRectMake(x, self.spacing, self.bounds.size.height-self.spacing*2, self.bounds.size.height-self.spacing*2);
    self.isOn = on;
    [self setColor];
}
- (void)setColor {
    if (self.on) {
        self.contentView.backgroundColor = self.selectedColor;
    }else{
        self.contentView.backgroundColor = self.defaultColor;
    }
}
- (void)layoutSubviews {
    [super layoutSubviews];
    self.contentView.frame = self.bounds;
    CGFloat x = self.on ? self.bounds.size.width-(self.bounds.size.height-self.spacing*2)-self.spacing : self.spacing;
    self.roundView.frame = CGRectMake(x, self.spacing, self.bounds.size.height-self.spacing*2, self.bounds.size.height-self.spacing*2);
    [self cornerRadius];
}
- (void)cornerRadius {
    if (self.contentView.layer.cornerRadius != self.bounds.size.height/2) {
        self.contentView.layer.cornerRadius = self.bounds.size.height/2;
    }
    if (self.roundView.layer.cornerRadius != self.roundView.bounds.size.height/2) {
        self.roundView.layer.cornerRadius = self.roundView.bounds.size.height/2;
    }
}
- (BOOL)on {
    return self.isOn;
}

- (void)translateAni {
    CGRect rect = self.roundView.frame;
    [UIView animateWithDuration:0.3 animations:^{
        self.roundView.frame = rect;
    }];
    if (!self.isOn){
        rect.origin.x = self.bounds.size.width - self.roundView.bounds.size.width - 2;
    }else{
        rect.origin.x = 2;
    }
    [UIView animateWithDuration:0.3 animations:^{
        self.roundView.frame = rect;
    }completion:^(BOOL finished) {
        self.on = !self.isOn;
    }];
}

//监听isON
//keyPath:属性名称
//object:被观察的对象
//change:变化前后的值都存储在change字典中
//context:注册观察者时，context传过来的值
-(void)observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(NSDictionary<NSString *,id> *)change context:(void *)context {
    [self.contentView sendActionsForControlEvents:UIControlEventValueChanged];
}

-(void)dealloc {
    [self.contentView removeObserver:self forKeyPath:@"isOn"];
    NSLog(@"释放");
}

@end
